package demo3;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/forward1")
public class ForwardServlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 1. 模拟业务逻辑
        String username = request.getParameter("username");
        String result = "业务处理完成：用户[" + (username == null ? "匿名" : username) + "]请求已接收";

        // 2. 存入请求域数据
        request.setAttribute("bizResult", result);
        request.setAttribute("count", 100);

        // 3. 请求转发到forward2
        RequestDispatcher dispatcher = request.getRequestDispatcher("/forward2");
        dispatcher.forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
